package com.glenet.salary.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;

import com.glenet.salary.entities.HrAgreement;
import com.glenet.salary.util.SessionFactoryUtil;

public class HrAgreementDao {

	@SuppressWarnings("unchecked")
	public List<HrAgreement> getAgreement(int offset, int limit){
		List<HrAgreement> list = new ArrayList<HrAgreement>(0);
		try {
			SqlSession session = SessionFactoryUtil.openSession();
			list = session.selectList("HrAgreement.list", null, new RowBounds(offset, limit));
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	public HrAgreement getAgreement(Long id){
		HrAgreement agreement = new HrAgreement();
		try {
			SqlSession session = SessionFactoryUtil.openSession();
			agreement = (HrAgreement)session.selectOne("HrAgreement.get", id);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return agreement;
	}
	
	public boolean save(HrAgreement agreement){
		boolean flag = false;
		try {
			SqlSession session = SessionFactoryUtil.openSession();
			flag = session.insert("HrAgreement.save", agreement) > 0 ? true : false;
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	public boolean delete(Long id){
		boolean flag = false;
		try {
			SqlSession session = SessionFactoryUtil.openSession();
			flag = session.delete("HrAgreement.delete", id) > 0 ? true : false;
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	public boolean update(HrAgreement agreement){
		boolean flag = false;
		try {
			SqlSession session = SessionFactoryUtil.openSession();
			flag = session.delete("HrAgreement.update", agreement) > 0 ? true : false;
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
}
